﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geodatabase;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using System.Data;


namespace Akiha.Core
{
    public static class FeatureClassWrapper
    {
        /// <summary>
        /// 通过属性查询获取到需要查询的要素
        /// </summary>
        /// <param name="pFeatureClass"></param>
        /// <param name="pFilter"></param>
        /// <returns></returns>
        public static List<IFeature> GetFeaturesFromFeatureClass(IFeatureClass FeatureClass, IQueryFilter Filter)
        {
            List<IFeature> lstFeature = new List<IFeature>();
            IFeatureCursor featureCursor = FeatureClass.Search(Filter, false);
            IFeature feature = featureCursor.NextFeature();
            try
            {
                while (feature != null)
                {
                    lstFeature.Add(feature);
                    feature = featureCursor.NextFeature();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
            finally
            {
                try
                {

                    Marshal.ReleaseComObject(featureCursor);
                    Marshal.ReleaseComObject(feature);
                }
                catch
                { }
                GC.Collect();
            }

            return lstFeature;
        }
        /// <summary>
        /// 通过空间查询获取到需要查询的要素
        /// </summary>
        /// <param name="pFeatureClass"></param>
        /// <param name="pFilter"></param>
        /// <returns></returns>
        public static List<IFeature> GetFeaturesFromFeatureClass(IFeatureClass FeatureClass, ISpatialFilter Filter)
        {
            List<IFeature> lstFeature = new List<IFeature>();
            IFeatureCursor featureCursor = FeatureClass.Search(Filter, false);
            IFeature feature = featureCursor.NextFeature();
            try
            {
                while (feature != null)
                {
                    lstFeature.Add(feature);
                    feature = featureCursor.NextFeature();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                throw;
            }
            finally
            {
                try
                {

                    Marshal.ReleaseComObject(featureCursor);
                    Marshal.ReleaseComObject(feature);
                }
                catch
                { }
                GC.Collect();
            }

            return lstFeature;
        }
        /// <summary>
        /// 根据某一个字段或许唯一值
        /// </summary>
        /// <param name="featureClass"></param>
        /// <param name="fieldName"></param>
        /// <returns></returns>
        public static List<object> GetUniqueValueDataStatistics(IFeatureClass featureClass, string fieldName)
        {
            List<object> lst = new List<object>();
            ICursor cursor = (ICursor)featureClass.Search(null, false);
            IDataStatistics dataStatistics = new DataStatisticsClass();
            dataStatistics.Field = fieldName;
            dataStatistics.Cursor = cursor;
            System.Collections.IEnumerator enumerator = dataStatistics.UniqueValues; enumerator.Reset();
            while (enumerator.MoveNext())
            {
                object myObject = enumerator.Current;
                lst.Add(myObject.ToString());
            }
            lst.Sort();

            try
            {

                Marshal.ReleaseComObject(cursor);
                Marshal.ReleaseComObject(dataStatistics);
            }
            catch
            { }
            GC.Collect();

            return lst;
        }
        /// <summary>
        /// 获取字段唯一值
        /// </summary>
        /// <param name="featureClass">图层</param>
        /// <param name="fieldName">字段名</param>
        /// <returns></returns>
        public static List<string> GetUniqueValueQueryDef(IFeatureClass featureClass, string fieldName)
        {
            ICursor cursor = null;
            try
            {
                List<string> lst = new List<string>();
                IDataset dataset = (IDataset)featureClass;
                IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)dataset.Workspace;
                IQueryDef queryDef = featureWorkspace.CreateQueryDef();
                queryDef.Tables = dataset.Name;
                queryDef.SubFields = "DISTINCT " + fieldName + "";
                cursor = queryDef.Evaluate();
                IRow row = cursor.NextRow();
                while (row != null)
                {
                    lst.Add(row.get_Value(0).ToString());
                    row = cursor.NextRow();
                }
                lst.Sort();
                return lst;
            }
            catch (Exception ex)
            {
                return new List<string>();
            }
            finally { if (cursor != null) Marshal.ReleaseComObject(cursor); }
        }
        /// <summary>
        /// 要素具体赋值
        /// </summary>
        /// <param name="pGeometry"></param>
        /// <param name="outputFeatureClass"></param>
        public static void LoadFeatureToFeatureClass(IFeatureClass outputFeatureClass, IGeometry pGeometry, Dictionary<string, object> attribute)
        {
            IFields intFields = outputFeatureClass.Fields;
            IDataset dataset = (IDataset)outputFeatureClass;
            IWorkspace workspace = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
            //start an edit session and operation              
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            IFeatureClassLoad pFCLoad = outputFeatureClass as IFeatureClassLoad;
            pFCLoad.LoadOnlyMode = true;
            IFeature tmpFea = outputFeatureClass.CreateFeature();
            try
            {
                if (attribute != null)
                {
                    foreach (KeyValuePair<string, object> kvp in attribute)
                    {
                        string fieldName = kvp.Key;
                        object fieldValue = kvp.Value;
                        tmpFea.set_Value(outputFeatureClass.FindField(fieldName), fieldValue);
                    }
                }
                if (pGeometry != null)
                {
                    ITopologicalOperator pTopologicalOperator = pGeometry as ITopologicalOperator;
                    pTopologicalOperator.Simplify();
                    tmpFea.Shape = pGeometry;
                    tmpFea.Store();
                    pFCLoad.LoadOnlyMode = false;
                }
                //stop editing  
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(intFields);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(dataset);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(tmpFea);
                }
                catch
                { }
                GC.Collect();
            }
        }

        public static void CopyFeatureToFeatureClass(IFeatureClass outputFeatureClass, IFeature feature, Dictionary<string, object> attribute)
        {
            try
            {
                IFields intFields = feature.Fields;
                IDataset dataset = (IDataset)outputFeatureClass;
                IWorkspace workspace = dataset.Workspace;
                IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
                //start an edit session and operation              
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                IFeatureClassLoad pFCLoad = outputFeatureClass as IFeatureClassLoad;
                pFCLoad.LoadOnlyMode = true;

                IFeature tmpFea = outputFeatureClass.CreateFeature();
                IFields outFields = outputFeatureClass.Fields;

                for (int i = 0; i < intFields.FieldCount; i++)
                {
                    IField field = intFields.get_Field(i);
                    string name = field.Name;
                    int fieldIndex;
                    object fieldValue;
                    if (field.Editable == false || field.Name.ToUpper() == "BGZT")
                        continue;
                    if (field.Name.ToUpper() == "SHAPE")
                    {
                        fieldIndex = intFields.FindField(name);//找到指定字段的索引
                        fieldValue = feature.get_Value(fieldIndex);//获得指定字段索引的字段值
                    }
                    fieldIndex = intFields.FindField(name);//找到指定字段的索引
                    fieldValue = feature.get_Value(fieldIndex);//获得指定字段索引的字段值
                    tmpFea.set_Value(outputFeatureClass.FindField(name), fieldValue);

                }
                if (attribute != null)
                {
                    foreach (KeyValuePair<string, object> kvp in attribute)
                    {
                        string fieldName = kvp.Key;
                        object fieldValue = kvp.Value;
                        tmpFea.set_Value(outputFeatureClass.FindField(fieldName), fieldValue);
                    }
                }
                IGeometry pGeometry = tmpFea.Shape;
                if (pGeometry == null)
                {
                    MessageBox.Show("要素图形为空！");
                    return;
                }
                if (pGeometry != null)
                {
                    ITopologicalOperator pTopologicalOperator = pGeometry as ITopologicalOperator;
                    pTopologicalOperator.Simplify();
                    tmpFea.Shape = pGeometry;
                    tmpFea.Store();
                    pFCLoad.LoadOnlyMode = false;
                }
                //stop editing  
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);

                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(intFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(outFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pGeometry);
                }
                catch
                { }
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }

        public static void CopyFeatureToFeatureClass(IFeatureClass outputFeatureClass, IFeature feature)
        {
            try
            {
                IFields intFields = feature.Fields;
                IDataset dataset = (IDataset)outputFeatureClass;
                IWorkspace workspace = dataset.Workspace;
                IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
                //start an edit session and operation              
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                IFeatureClassLoad pFCLoad = outputFeatureClass as IFeatureClassLoad;
                pFCLoad.LoadOnlyMode = true;

                IFeature tmpFea = outputFeatureClass.CreateFeature();
                IFields outFields = outputFeatureClass.Fields;

                for (int i = 0; i < intFields.FieldCount; i++)
                {
                    IField field = intFields.get_Field(i);
                    string name = field.Name;
                    int fieldIndex;
                    object fieldValue;
                    if (field.Editable == false || field.Name.ToUpper() == "BGZT")
                        continue;
                    if (field.Name.ToUpper() == "SHAPE")
                    {
                        fieldIndex = intFields.FindField(name);//找到指定字段的索引
                        fieldValue = feature.get_Value(fieldIndex);//获得指定字段索引的字段值
                    }
                    fieldIndex = intFields.FindField(name);//找到指定字段的索引
                    fieldValue = feature.get_Value(fieldIndex);//获得指定字段索引的字段值
                    tmpFea.set_Value(outputFeatureClass.FindField(name), fieldValue);

                }
                IGeometry pGeometry = tmpFea.Shape;
                if (pGeometry == null)
                {
                    MessageBox.Show("要素图形为空！");
                    return;
                }
                if (pGeometry != null)
                {
                    ITopologicalOperator pTopologicalOperator = pGeometry as ITopologicalOperator;
                    pTopologicalOperator.Simplify();
                    tmpFea.Shape = pGeometry;
                    tmpFea.Store();
                    pFCLoad.LoadOnlyMode = false;
                }
                //stop editing  
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);

                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(intFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(tmpFea);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(outFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(pGeometry);
                }
                catch
                { }
                GC.Collect();
            }
            catch (Exception ex)
            {

            }
        }


        //游标法的添加要素
        //加载大量简单数据有速度优势
        public static bool AddFeatureToFeatureClass(IFeatureClass outputFeatureClass, List<IFeature> lstfeas)
        {
            try
            {
                IDataset dataset = (IDataset)outputFeatureClass;
                IWorkspace workspace = dataset.Workspace;
                IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
                //start an edit session and operation              
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                foreach (IFeature feature in lstfeas)
                {
                    IFeatureCursor pFeaCusor = outputFeatureClass.Insert(false);
                    pFeaCusor.InsertFeature(feature as IFeatureBuffer);
                }
                 
                //暂时未设定图层范围的更新
                ////更新图层范围
                //IFeatureClassManage featureClassManage = outputFeatureClass as IFeatureClassManage;
                //featureClassManage.UpdateExtent();

                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);


                //System.Runtime.InteropServices.Marshal.ReleaseComObject(intFields);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(tmpFea);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(outFields);
                //System.Runtime.InteropServices.Marshal.ReleaseComObject(pGeometry);
             
                GC.Collect();
                return true;
            }
            catch 
            {
                return false;
            }
        }




        //使用数据绑定中提供的方式
        public static void AddFeatureToFeatureClass()
        {

        }

        


        //添加多字段
        public static void AddFieldsToFeatureClass(IFeatureClass feacls, List<IField> lstfield)
        {

        }

        //添加单字段
        public static void AddFieldToFeatureClass(IFeatureClass feacls, IField pfield)
        {

        }




        /// <summary>
        /// 删除要素
        /// </summary>
        /// <param name="feature"></param>
        public static void DelFeature(IFeature pFeature)
        {
            IFeatureClass featureClass = null;
            IDataset dataset = null;
            IWorkspace workspace = null;
            IWorkspaceEdit workspaceEdit = null;
            try
            {

                featureClass = pFeature.Class as IFeatureClass;
                dataset = (IDataset)featureClass;
                workspace = dataset.Workspace;
                workspaceEdit = (IWorkspaceEdit)workspace;
                string featureClassName = dataset.Name;

                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                pFeature.Delete();

                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);

            }
            catch (Exception ex)
            {

            }
            finally
            {
                try
                {
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeature);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(featureClass);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(dataset);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                }
                catch
                { }
                GC.Collect();
            }
        }
        /// <summary>
        /// 删除要素
        /// </summary>
        /// <param name="feature"></param>
        public static void DelFeatures(List<IFeature> features)
        {
            IFeature pFeature = null;
            IFeatureClass featureClass = null;
            IDataset dataset = null;
            IWorkspace workspace = null;
            IWorkspaceEdit workspaceEdit = null;
            try
            {
                for (int i = 0; i < features.Count; i++)
                {
                    pFeature = features[i] as IFeature;
                    featureClass = pFeature.Class as IFeatureClass;
                    dataset = (IDataset)featureClass;
                    workspace = dataset.Workspace;
                    workspaceEdit = (IWorkspaceEdit)workspace;
                    string featureClassName = dataset.Name;

                    workspaceEdit.StartEditing(true);
                    workspaceEdit.StartEditOperation();

                    pFeature.Delete();

                    workspaceEdit.StopEditOperation();
                    workspaceEdit.StopEditing(true);
                }
            }
            catch (Exception ex)
            {

            }
            finally
            {
                try
                {
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeature);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(dataset);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    //System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                }
                catch
                { }
                GC.Collect();
            }
        }

        /// <summary>
        /// 使用游标删除要素
        /// </summary>
        /// <param name="featureClass"></param>
        /// <param name="sql"></param>
        public static void DelFeatureUseCursor(IFeatureClass featureClass, string sql)
        {
            try
            {
                IWorkspace workspace = ((IDataset)featureClass).Workspace;
                IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                IQueryFilter queryFilter = new QueryFilterClass();
                queryFilter.WhereClause = sql;
                IFeatureCursor updateCursor = featureClass.Update(queryFilter, false);
                IFeature feature = updateCursor.NextFeature();

                while (feature != null)
                {
                    updateCursor.DeleteFeature();
                    feature = updateCursor.NextFeature();
                }

                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);

                System.Runtime.InteropServices.Marshal.ReleaseComObject(queryFilter);
                System.Runtime.InteropServices.Marshal.ReleaseComObject(updateCursor);
            }
            catch (Exception ex)
            {
            }
            finally
            {

                GC.Collect();
            }
        }

        public static void DelFeatures(IFeatureClass featureClass, List<IFeature> features)
        {
            IDataset dataset = featureClass as IDataset;
            IWorkspace workspace = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;
            try
            {
                for (int i = 0; i < features.Count; i++)
                {

                    workspaceEdit.StartEditing(true);
                    workspaceEdit.StartEditOperation();

                    features[i].Delete();

                    workspaceEdit.StopEditOperation();
                    workspaceEdit.StopEditing(true);
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 要素具体赋值
        /// </summary>
        /// <param name="pGeometry"></param>
        /// <param name="outputFeatureClass"></param>
        public static void UpdateFeatureValues(IFeatureClass outputFeatureClass, IFeature feature, Dictionary<string, object> attribute)
        {
            IFields intFields = outputFeatureClass.Fields;
            IDataset dataset = (IDataset)outputFeatureClass;
            IWorkspace workspace = dataset.Workspace;
            IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;
            //start an edit session and operation              
            workspaceEdit.StartEditing(true);
            workspaceEdit.StartEditOperation();
            try
            {
                if (attribute != null)
                {
                    foreach (KeyValuePair<string, object> kvp in attribute)
                    {
                        string fieldName = kvp.Key;
                        object fieldValue = kvp.Value;
                        feature.set_Value(outputFeatureClass.FindField(fieldName), fieldValue);
                    }
                }
                feature.Store();
                //stop editing  
                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            finally
            {
                try
                {
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(intFields);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspace);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(workspaceEdit);
                }
                catch
                { }
                GC.Collect();
            }
        }
        /// <summary>
        /// 获取SDEFeatureClass
        /// </summary>
        /// <param name="strSDETableName">SDE表名称</param>
        /// <param name="pWorkspace">IWorkspace</param>
        /// <returns>IFeatureClass</returns>
        public static IFeatureClass GetSDEFeatureClass(string strSDETableName, IFeatureWorkspace pFeatureWorkspace)
        {
            if (string.IsNullOrEmpty(strSDETableName)) return null;
            if (pFeatureWorkspace == null) return null;
            IFeatureClass pSDEFeatureClass = pFeatureWorkspace.OpenFeatureClass(strSDETableName);
            return pSDEFeatureClass;
        }

        public static IWorkspace GetWorkspace(IFeatureClass pFeatureClass)
        {
            if (pFeatureClass == null)
            {
                return null;
            }
            IDataset pDataset = pFeatureClass as IDataset;
            if (pDataset == null)
            {
                return null;
            }
            return pDataset.Workspace;
        }

        public static bool SupportUndoRedoEditing(IFeatureClass featureClass)
        {
            IWorkspace ws = GetWorkspace(featureClass);
            if (ws == null)
            {
                return false;
            }
            if (ws.Type == esriWorkspaceType.esriRemoteDatabaseWorkspace)
            {
                IVersionedObject3 versionedObject = featureClass as IVersionedObject3;
                return versionedObject.IsRegisteredAsVersioned;
            }
            return true;
        }

        public static void StartEditOperation(IFeatureClass featureClass)
        {
            IWorkspace ws = GetWorkspace(featureClass);
            if (ws != null)
            {
                IWorkspaceEdit wse = ws as IWorkspaceEdit;
                if ((wse != null) && wse.IsBeingEdited())
                {
                    if (SupportUndoRedoEditing(featureClass))
                    {
                        wse.StartEditOperation();
                    }
                    else
                    {
                        bool hasEdits = false;
                        wse.HasEdits(ref hasEdits);
                        if (!hasEdits)
                        {
                            wse.StartEditOperation();
                        }
                    }
                }
            }
        }

        public static void StopEditOperation(IFeatureClass featureClass)
        {
            IWorkspace ws = GetWorkspace(featureClass);
            if (ws != null)
            {
                IWorkspaceEdit wse = ws as IWorkspaceEdit;
                if (((wse != null) && wse.IsBeingEdited()) && SupportUndoRedoEditing(featureClass))
                {
                    wse.StopEditOperation();
                }
            }
        }

        public static double GetMaxValue(IFeatureClass pFeatClass, string fieldName)
        {
            ICursor cursor = (ICursor)pFeatClass.Search(null, false);
            IDataStatistics dataStat = new DataStatisticsClass();
            dataStat.Field = fieldName;
            dataStat.Cursor = cursor;
            IStatisticsResults results = dataStat.Statistics;

            return results.Maximum;
        }

        public static List<IFeature> GetPointFeatureFromShapeContains(IFeatureClass sourceFeatureClass, IGeometry Geometry, double distance)
        {
            IBufferConstruction bc = new BufferConstruction();
            IGeometry geometry = null;
            if (distance != null)
            {
                geometry = bc.Buffer(Geometry, distance);
            }
            else
            {
                geometry = bc.Buffer(Geometry, 0.001);
            }
            if (Geometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                IEnvelope env = new EnvelopeClass() as IEnvelope;
                if (distance != null)
                {
                    env.PutCoords(Geometry.Envelope.XMin - distance, Geometry.Envelope.YMin - distance, Geometry.Envelope.XMax + distance, Geometry.Envelope.YMax + distance);
                    geometry = env as IGeometry;
                }
                else
                {
                    env.PutCoords(Geometry.Envelope.XMin - 0.001, Geometry.Envelope.YMin - 0.001, Geometry.Envelope.XMax + 0.001, Geometry.Envelope.YMax + 0.001);
                    geometry = env as IGeometry;
                }
            }
            ISpatialFilter spatialFilter = new SpatialFilterClass();
            spatialFilter.Geometry = geometry;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains;

            List<IFeature> listFeature = FeatureClassWrapper.GetFeaturesFromFeatureClass(sourceFeatureClass, spatialFilter);

            return listFeature;
        }

        public static IFeature UpdateGeometry(IFeatureClass featureClass, IFeature feature, IGeometry geometry)
        {
            try
            {
                IDataset dataset = (IDataset)featureClass;
                IWorkspace workspace = dataset.Workspace;
                IWorkspaceEdit workspaceEdit = workspace as IWorkspaceEdit;
                workspaceEdit.StartEditing(true);
                workspaceEdit.StartEditOperation();

                feature.Shape = geometry;
                feature.Store();

                workspaceEdit.StopEditOperation();
                workspaceEdit.StopEditing(true);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                GC.Collect();
            }
            return feature;
        }


    }
}
